<template>
  <div>
    <!-- 面包屑导航 -->
    <el-breadcrumb separator="/">
      <el-breadcrumb-item :to="{ path: '/home' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item>订单管理</el-breadcrumb-item>
      <el-breadcrumb-item>订单列表</el-breadcrumb-item>
    </el-breadcrumb>
    <!-- 卡片视图区域 -->
    <el-card>
      <el-row>
        <el-col :span="8">
          <el-input placeholder="请输入内容" v-model="queryInfo.query" @input="getOrderList" clearable>
            <el-button slot="append" icon="el-icon-search" @click="getOrderList"></el-button>
          </el-input>
        </el-col>
      </el-row>
      <el-table :data="orderLsit" border stripe>
        <el-table-column type="index" label="#" width="60" align="center"></el-table-column>
        <el-table-column prop="order_number" label="订单编号"></el-table-column>
        <el-table-column prop="order_price" label="订单价格" width="120" align="center"></el-table-column>
        <el-table-column prop="pay_status" label="是否付款" width="120" align="center">
          <template slot-scope="scoped">
            <el-tag type="danger" v-if="scoped.row.pay_status == '0'">未付款</el-tag>
            <el-tag type="success" v-else>已付款</el-tag>
          </template>
        </el-table-column>
        <el-table-column prop="is_send" label="是否发货" width="120" align="center">
          <template slot-scope="scoped">
            <el-tag type="danger" v-if="scoped.row.is_send == '否'">未发货</el-tag>
            <el-tag type="success" v-else>已发货</el-tag>
          </template>
        </el-table-column>
        <el-table-column label="下单时间" width="180" align="center">
          <template slot-scope="scoped">{{scoped.row.create_time | dataFormate}}</template>
        </el-table-column>
        <el-table-column label="操作" width="120" align="center">
          <el-button type="primary" icon="el-icon-edit" size="mini" @click="showEditAddress"></el-button>
          <el-button
            type="success"
            icon="el-icon-map-location"
            size="mini"
            @click="showProgressDialog"
          ></el-button>
        </el-table-column>
      </el-table>
      <!-- 分页区域 -->
      <el-pagination
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="queryInfo.pagenum"
        :page-sizes="[1, 5, 10]"
        :page-size="queryInfo.pagesize"
        layout="total, sizes, prev, pager, next, jumper"
        :total="totalPage"
      ></el-pagination>
    </el-card>
    <!-- 修改地址对话框 -->
    <el-dialog
      title="修改地址"
      :visible.sync="addressdialogVisible"
      width="30%"
      @close="addressDialogClosed"
    >
      <el-form
        :model="addressInfo"
        :rules="addressInfoRul"
        ref="addressInfoRef"
        label-width="100px"
      >
        <el-form-item label="省市区/县" prop="address1">
          <el-cascader
            v-model="addressInfo.address1"
            :options="citydata"
            :props="{ expandTrigger: 'hover' }"
            clearable
          ></el-cascader>
        </el-form-item>
        <el-form-item label="详细地址" prop="address2">
          <el-input v-model="addressInfo.address2"></el-input>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="addressdialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="addressdialogVisible = false">确 定</el-button>
      </span>
    </el-dialog>
    <!-- 物流信息对话框 -->
    <el-dialog title="最新物流" :visible.sync="ProgressDialogVisible" width="30%">
      <!-- 物流信息时间线 -->
      <el-timeline>
        <el-timeline-item
          v-for="(item, index) in Logistics"
          :key="index"
          :timestamp="item.ftime"
        >{{item.context}}</el-timeline-item>
      </el-timeline>
      <span slot="footer" class="dialog-footer">
        <el-button @click="ProgressDialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="ProgressDialogVisible = false">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>
<script>
import citydata from './citydata'
export default {
  data() {
    return {
      queryInfo: {
        //   查询参数
        query: '',
        //   当前页码
        pagenum: 1,
        //   每页显示条数
        pagesize: 5
      },
      //   订单列表
      orderLsit: [],
      //   总条数
      totalPage: 0,
      //   修改地址对话框的显示隐藏
      addressdialogVisible: false,
      //  物流信息对话框的显示隐藏
      ProgressDialogVisible: false,
      //   地址信息列表
      addressInfo: {
        address1: [],
        address2: ''
      },
      //   物流信息
      Logistics: [],
      //   地址信息
      citydata,
      //   地址信息的校验规则
      addressInfoRul: {
        address1: [
          { required: true, message: '请选择省市区/县', trigger: 'blur' }
        ],
        address2: [
          { required: true, message: '请输入详细地址', trigger: 'blur' }
        ]
      }
    }
  },
  created() {
    //   获取订单列表
    this.getOrderList()
  },
  methods: {
    //   获取订单列表
    async getOrderList() {
      const { data: res } = await this.$http.get('orders', {
        params: this.queryInfo
      })
      if (res.meta.status !== 200) {
        return this.$message.error('获取订单列表失败！')
      }
      this.totalPage = res.data.total
      this.orderLsit = res.data.goods
    },
    // 每页显示条数的响应函数
    handleSizeChange(pageSize) {
      this.queryInfo.pagesize = pageSize
      this.getOrderList()
    },
    // 当前页的响应函数
    handleCurrentChange(currentPage) {
      this.queryInfo.pagenum = currentPage
      this.getOrderList()
    },
    // 点击修改按钮修改地址
    showEditAddress() {
      this.addressdialogVisible = true
    },
    // 监听修改地址对话框的关闭
    addressDialogClosed() {
      // 对整个表单进行重置，将所有字段值重置为初始值并移除校验结果
      this.$refs.addressInfoRef.resetFields()
    },
    // 展示物流信息对话框
    async showProgressDialog() {
      this.ProgressDialogVisible = true
      //   发起请求，获取物流信息
      const { data: res } = await this.$http.get(`/kuaidi/${1106975712662}`)
      console.log(res)
      this.Logistics = res.data
    }
  }
}
</script>
<style lang="less" scoped>
</style>
